package com.lb.shopguide.ui.dialog;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lb.baselib.base.DialogBase;
import com.lb.shopguide.R;
import com.lb.shopguide.constants.AppConstant;


/**
 * Created by kang on 2017/7/6.
 */

public class DialogLbCommon extends DialogBase implements View.OnClickListener {

    private TextView tvContent;
    private LinearLayout layoutTwoButton;
    private TextView tvLeft;
    private TextView tvRight;
    private TextView tvButtonBottom;

    private int mode;

    private String buttonText = "确定";
    private String dialogContent;
    private String leftText = "取消";
    private String rightText = "确定";

    public static DialogLbCommon getDialogLbCommon(int mode, String dialogContent, String buttonText) {
        DialogLbCommon dialogLbCommon = new DialogLbCommon();
        Bundle bundle = new Bundle();
        bundle.putInt(AppConstant.DIALOG_TYPE, mode);
        if (!TextUtils.isEmpty(buttonText)) {
            bundle.putString(AppConstant.DIALOG_BUTTON_TEXT, buttonText);
        }
        if (!TextUtils.isEmpty(dialogContent)) {
            bundle.putString(AppConstant.DIALOG_CONTENT, dialogContent);
        }
        dialogLbCommon.setArguments(bundle);
        return dialogLbCommon;
    }

    public static DialogLbCommon getDialogLbCommon(int mode, String dialogContent, String leftText, String rightText) {
        DialogLbCommon dialogLbCommon = new DialogLbCommon();
        Bundle bundle = new Bundle();
        bundle.putInt(AppConstant.DIALOG_TYPE, mode);
        if (!TextUtils.isEmpty(dialogContent)) {
            bundle.putString(AppConstant.DIALOG_CONTENT, dialogContent);
        }
        if (!TextUtils.isEmpty(leftText)) {
            bundle.putString(AppConstant.DIALOG_LEFT_TEXT, leftText);
        }
        if (!TextUtils.isEmpty(rightText)) {
            bundle.putString(AppConstant.DIALOG_RIGHT_TEXT, rightText);
        }
        dialogLbCommon.setArguments(bundle);
        return dialogLbCommon;
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
            savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        View mFragmentView = inflater.inflate(R.layout.dialog_lb_common, container, false);
        initData();
        initView(mFragmentView);

        return mFragmentView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (dialog != null) {
            DisplayMetrics dm = new DisplayMetrics();
            getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
            dialog.getWindow().setLayout((int) (dm.widthPixels * 0.8), ViewGroup.LayoutParams.WRAP_CONTENT);
            dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        }
    }

    private void initData() {
        Bundle bundle = getArguments();
        if (bundle.containsKey(AppConstant.DIALOG_TYPE)) {
            mode = bundle.getInt(AppConstant.DIALOG_TYPE);
        }
        if (bundle.containsKey(AppConstant.DIALOG_BUTTON_TEXT)) {
            buttonText = bundle.getString(AppConstant.DIALOG_BUTTON_TEXT);
        }
        if (bundle.containsKey(AppConstant.DIALOG_CONTENT)) {
            dialogContent = bundle.getString(AppConstant.DIALOG_CONTENT);
        }
        if (bundle.containsKey(AppConstant.DIALOG_LEFT_TEXT)) {
            leftText = bundle.getString(AppConstant.DIALOG_LEFT_TEXT);
        }
        if (bundle.containsKey(AppConstant.DIALOG_RIGHT_TEXT)) {
            rightText = bundle.getString(AppConstant.DIALOG_RIGHT_TEXT);
        }

    }

    private void initView(View view) {
        tvContent = view.findViewById(R.id.tv_dialog_content);
        layoutTwoButton = view.findViewById(R.id.layout_two_button);
        tvLeft = view.findViewById(R.id.tv_title);
        tvRight = view.findViewById(R.id.tv_right);
        tvButtonBottom = view.findViewById(R.id.tv_one_button);
        if (mode == AppConstant.DIALOG_COMMON_INFO) {
            tvButtonBottom.setVisibility(View.VISIBLE);
        } else if (mode == AppConstant.DIALOG_COMMON_CHOOSE) {
            layoutTwoButton.setVisibility(View.VISIBLE);
            tvLeft.setText(leftText);
            tvRight.setText(rightText);
        }
        tvButtonBottom.setText(buttonText);
        tvContent.setText(dialogContent);

        initListener();
    }

    private void initListener() {
        tvLeft.setOnClickListener(this);
        tvRight.setOnClickListener(this);
        tvButtonBottom.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_title:
                if (mOnTwoButtonClickListener != null) {
                    mOnTwoButtonClickListener.onLeftClick();
                }
                break;
            case R.id.tv_right:
                if (mOnTwoButtonClickListener != null) {
                    mOnTwoButtonClickListener.onRightClick();
                }
                break;
            case R.id.tv_one_button:
                if (mOnOneButtonClickListener != null) {
                    mOnOneButtonClickListener.onOneButtonClick();
                }
                DialogLbCommon.this.dismiss();
                break;
        }
    }

    public interface OnOneButtonClickListener {
        void onOneButtonClick();
    }

    private OnOneButtonClickListener mOnOneButtonClickListener;

    public void setOnOneButtonClickListener(OnOneButtonClickListener listener) {
        mOnOneButtonClickListener = listener;
    }

    public interface OnTwoButtonClickListener {
        void onLeftClick();

        void onRightClick();
    }

    private OnTwoButtonClickListener mOnTwoButtonClickListener;

    public void setOnTwoButtonClickListener(OnTwoButtonClickListener listener) {
        mOnTwoButtonClickListener = listener;
    }
}
